package org.jeecg.modules.solitaire_app.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.solitaire_app.app.entity.YsyProductionMarketing;
import org.jeecg.modules.solitaire_app.app.service.IYsyProductionMarketingService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 产销比数据
 * @Author: jeecg-boot
 * @Date: 2024-04-07
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "产销比数据")
@RestController
@RequestMapping("/app/ysyProductionMarketing")
public class YsyProductionMarketingController extends JeecgController<YsyProductionMarketing, IYsyProductionMarketingService> {
    @Autowired
    private IYsyProductionMarketingService ysyProductionMarketingService;

    /**
     * 分页列表查询
     *
     * @param ysyProductionMarketing
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "产销比数据-分页列表查询")
    @ApiOperation(value = "产销比数据-分页列表查询", notes = "产销比数据-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsyProductionMarketing ysyProductionMarketing,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YsyProductionMarketing> queryWrapper = QueryGenerator.initQueryWrapper(ysyProductionMarketing, req.getParameterMap());
        Page<YsyProductionMarketing> page = new Page<YsyProductionMarketing>(pageNo, pageSize);
        queryWrapper.orderByDesc("create_time");
        IPage<YsyProductionMarketing> pageList = ysyProductionMarketingService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 产销比数据-折线图，下标0-汽油，1-柴油，2-行业早报，3-热点聚集
     *
     * @param
     * @return
     */
    @AutoLog(value = "产销比数据-折线图，下标0-汽油，1-柴油，2-行业早报，3-热点聚集")
    @ApiOperation(value = "产销比数据-折线图，下标0-汽油，1-柴油，2-行业早报，3-热点聚集", notes = "产销比数据-折线图，下标0-汽油，1-柴油，2-行业早报，3-热点聚集")
    @GetMapping(value = "/getYsyProductionMarketingKline")
    public Result<JSONArray> getYsyProductionMarketingKline() {
        return ysyProductionMarketingService.getYsyProductionMarketing();
    }

    /**
     * 产销比数据-顶部产销比数据
     *
     * @param
     * @return
     */
    @AutoLog(value = "产销比数据-顶部产销比数据")
    @ApiOperation(value = "产销比数据-顶部产销比数据", notes = "产销比数据-顶部产销比数据")
    @GetMapping(value = "/getTopNewProductionMarketing")
    public Result<Map<String, Object>> getTopNewProductionMarketing() {
        return ysyProductionMarketingService.getTopNewProductionMarketing();
    }

    /**
     * 添加
     *
     * @param ysyProductionMarketing
     * @return
     */
    @AutoLog(value = "产销比数据-添加")
    @ApiOperation(value = "产销比数据-添加", notes = "产销比数据-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsyProductionMarketing ysyProductionMarketing) {

        YsyProductionMarketing ysyProductionMarketingInsert = new YsyProductionMarketing();
        ysyProductionMarketingInsert.setProportion(ysyProductionMarketing.getProportion());

        //查询上次
        YsyProductionMarketing ysyProductionMarketingLastOne = ysyProductionMarketingService
                .getOne(Wrappers.<YsyProductionMarketing>lambdaQuery()
                        .eq(YsyProductionMarketing::getType, ysyProductionMarketing.getType())
                        .orderByDesc(YsyProductionMarketing::getCreateTime)
                        .last("limit 1"));
        if (ObjectUtil.isNotEmpty(ysyProductionMarketingLastOne)) {
            ysyProductionMarketingInsert.setUpsDowns((Integer.parseInt(ysyProductionMarketing.getProportion())
                    - Integer.parseInt(ysyProductionMarketingLastOne.getProportion())) + "");
            ysyProductionMarketingInsert.setUpsDownsStatus(Integer.parseInt(ysyProductionMarketingInsert.getUpsDowns()) < 0 ? "2" : "1");
            ysyProductionMarketingInsert.setStatus(ysyProductionMarketingInsert.getUpsDownsStatus());
        }
        ysyProductionMarketingInsert.setType(ysyProductionMarketing.getType());
        ysyProductionMarketingInsert.setTypeName(Integer.parseInt(ysyProductionMarketing.getType()) == 1 ? "汽油" : "柴油");
        ysyProductionMarketingInsert.setCreateTime(new Date());
        ysyProductionMarketingInsert.setUpdateTime(new Date());
        ysyProductionMarketingService.save(ysyProductionMarketingInsert);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ysyProductionMarketing
     * @return
     */
    @AutoLog(value = "产销比数据-编辑")
    @ApiOperation(value = "产销比数据-编辑", notes = "产销比数据-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsyProductionMarketing ysyProductionMarketing) {
        ysyProductionMarketingService.remove(Wrappers.<YsyProductionMarketing>lambdaQuery()
                .eq(YsyProductionMarketing::getProductionMarketingId, ysyProductionMarketing.getProductionMarketingId()));
        YsyProductionMarketing ysyProductionMarketingInsert = new YsyProductionMarketing();
        ysyProductionMarketingInsert.setProportion(ysyProductionMarketing.getProportion());

        //查询上次
        YsyProductionMarketing ysyProductionMarketingLastOne = ysyProductionMarketingService
                .getOne(Wrappers.<YsyProductionMarketing>lambdaQuery()
                        .eq(YsyProductionMarketing::getType, ysyProductionMarketing.getType())
                        .orderByDesc(YsyProductionMarketing::getCreateTime)
                        .last("limit 1"));
        if (ObjectUtil.isNotEmpty(ysyProductionMarketingLastOne)) {
            ysyProductionMarketingInsert.setUpsDowns((Integer.parseInt(ysyProductionMarketing.getProportion())
                    - Integer.parseInt(ysyProductionMarketingLastOne.getProportion())) + "");
            ysyProductionMarketingInsert.setUpsDownsStatus(Integer.parseInt(ysyProductionMarketingInsert.getUpsDowns()) < 0 ? "2" : "1");
            ysyProductionMarketingInsert.setStatus(ysyProductionMarketingInsert.getUpsDownsStatus());
        }
        ysyProductionMarketingInsert.setType(ysyProductionMarketing.getType());
        ysyProductionMarketingInsert.setTypeName(Integer.parseInt(ysyProductionMarketing.getType()) == 1 ? "汽油" : "柴油");
        ysyProductionMarketingInsert.setCreateTime(new Date());
        ysyProductionMarketingInsert.setUpdateTime(new Date());
        ysyProductionMarketingService.save(ysyProductionMarketingInsert);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产销比数据-通过id删除")
    @ApiOperation(value = "产销比数据-通过id删除", notes = "产销比数据-通过id删除")
    @GetMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable String id) {
        ysyProductionMarketingService.remove(Wrappers.<YsyProductionMarketing>lambdaQuery()
                .eq(YsyProductionMarketing::getProductionMarketingId, id));
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "产销比数据-批量删除")
    @ApiOperation(value = "产销比数据-批量删除", notes = "产销比数据-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ysyProductionMarketingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产销比数据-通过id查询")
    @ApiOperation(value = "产销比数据-通过id查询", notes = "产销比数据-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YsyProductionMarketing ysyProductionMarketing = ysyProductionMarketingService.getById(id);
        return Result.ok(ysyProductionMarketing);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysyProductionMarketing
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsyProductionMarketing ysyProductionMarketing) {
        return super.exportXls(request, ysyProductionMarketing, YsyProductionMarketing.class, "产销比数据");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsyProductionMarketing.class);
    }

}
